import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
    通过反射机制调用方法
 */
public class ReflectTest06 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class userServiceClass = Class.forName("UserService");
        Object obj = userServiceClass.newInstance();

        //获取Method
        Method loginMethod = userServiceClass.getDeclaredMethod("login", String.class , String.class);
        //调用方法
        /*
        四要素：
            loginMethod 方法
            obj 对象
            "admin", "123" 实参
            retValue 返回值
         */
        //调用方法用invoke，调用构造方法new对象依然用newInstance(参数)
        Object retValue = loginMethod.invoke(obj, "admin", "123");
        System.out.println(retValue);
    }
}
